//
//  ViewController.m
//  10-团购
//
//  Created by wangneihan on 15/12/18.
//  Copyright © 2015年 wangneihan. All rights reserved.
//

#import "ViewController.h"
#import "GroupBuying.h"
#import "gbTableViewCell.h"
#import "gbFootView.h"
#import "gbHeaderBiew.h"

@interface ViewController () <UITableViewDataSource,gbFootViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic,strong) NSMutableArray *array;

@property (nonatomic ,assign) NSInteger currentIndex;

@property (nonatomic ,assign) NSInteger pageSize;

@end

@implementation ViewController

-(NSMutableArray *)array
{
    if(_array == nil)
    {
        _array = [GroupBuying groupBuyingsList];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    self.pageSize = 2;
    gbFootView *footView = [gbFootView loadGbFootView];
    footView.delegate = self;
    gbHeaderBiew *headerView = [gbHeaderBiew headerBiew];
    self.tableView.tableHeaderView = headerView;
    self.tableView.tableFooterView = footView;
    // Do any additional setup after loading the view, typically from a nib.
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return  self.array.count > self.pageSize ? self.pageSize:self.array.count;
    return  self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.currentIndex ++ ;
    
    gbTableViewCell *cell = [gbTableViewCell gbTableViewCellWithTableView:self.tableView];
    
    cell.groupBuying = self.array[indexPath.row];
    
    return cell;
}

-(void)gbfootViewDidClickedLoadMore:(gbFootView *)footView
{
//    NSInteger nextIndex = self.array.count > self.currentIndex + self.pageSize ? self.currentIndex + self.pageSize : self.array.count;
//    
//    for (self.currentIndex; self.currentIndex < nextIndex ; self.currentIndex ++) {
//       
//        GroupBuying *groupBuying = self.array[self.currentIndex];
//        gbTableViewCell *cell = [gbTableViewCell gbTableViewCellWithTableView:self.tableView];
//        cell.groupBuying = groupBuying;
//        
//        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.currentIndex-1 inSection:0];
//        
//        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//        
//        [self.tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:NO];
//
//    }
//    
//    NSLog(@"代理点击了！");
    GroupBuying *gb = [[GroupBuying alloc] init];
    gb.title = @"牛腩王";
    gb.price = @"15";
    gb.buyCount = @"1000";
    gb.icon = @"2c97690e72365e38e3e2a95b934b8dd2";
    //
    [self.array addObject:gb];
    
    //    [self.tableView reloadData];
    
    //插入新的cell
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.array.count - 1 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    //滚动到指定cell的底部
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    

}

@end
